home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1993 / Internet Info CD-ROM (Walnut Creek) (1993).iso / networking / cisco / acctsum.perl < prev    next >
Encoding:
Text File  |  1992-08-07  |  4.0 KB  |  154 lines

  1. #!/usr/local/bin/perl
  2. #
  3. #    acctsum    - Summarize SNMP Accounting info from cisco routers
  4. #     Author: Carl Rigney; cdr@bach.amd.com; 90/7/23
  5. #
  6. # Modification History
  7. # 1.0    90/10/16 cdr    initial release
  8.  
  9. # Boilerplate:
  10. # Copyright 1990 Carl Rigney.  You may redistribute freely in any form.
  11. #
  12. # If you make any interesting improvements to this script I'd enjoy hearing
  13. # about them.
  14.  
  15. # Synopsis:
  16. # This script runs CMU snmpwalk to retrieve lipAccountEntry from cisco
  17. # routers to show packet & byte counts for each pair of machines that
  18. # have communicated through the router using IP since last reboot.  
  19. # It produces a tab-separated table of
  20. # source    destination    #packets    #bytes
  21. #
  22. # If a hosts table is present it will place an asterisk by addresses it
  23. # doesn't find in the hosts table.  You can use YP instead with -y.
  24.  
  25.  
  26. $logfile="/usr/tmp/snmp$$";
  27. $unknown="*";            # character to flag unknown hosts with
  28.  
  29. $mibvar = ".iso.org.dod.internet.private.enterprises.cisco.local.lip.lipAccoun
  30. tingTable.lipAccountEntry";
  31. $prefix = "Name: ".$mibvar.".";    # what CMU SNMP Tools output
  32.  
  33.  
  34. ### Parse Command-line options
  35.  
  36. $log=0;                # flag to log snmp output to $logfile
  37. $name=1;            # flag to use names of known hosts
  38. $nis=0;                # flag to use Network Information Services (YP
  39. )
  40.  
  41. while ($ARGV[0] =~ m/^-(.)/) {
  42.     $log++ if $1 eq "l";    # -l    log to $logfile
  43.     $name=0 if $1 eq "n";    # -n    use IP addresses for known hosts too
  44.     $nis++ if $1 eq "y";    # -y    use NIS (YP) to find known IP addresse
  45. s
  46.     shift @ARGV;
  47. }
  48.  
  49. if ($nis) {
  50.     $hostfile="ypcat hosts|";     # to use YP
  51. } else {
  52.     $hostfile="/etc/hosts";        # to use hostsfile
  53. }
  54.  
  55.  
  56. ### Retrieve Valid Hostnames
  57.  
  58. # we don't use a Domain nameserver so someone else will have to adapt
  59. # this to use in-addr.arpa lookups if they want to use those.
  60.  
  61. # slurp in hosts file to find valid IP addresses
  62. # run this only on hosts with YP or up-to-date hosts files, as chosen above
  63.  
  64. open(HOSTS,$hostfile) || die "$0: couldn't read $hostfile;$!\n";
  65. while (<HOSTS>) {
  66.     chop;
  67.     s/#.*//;    # strip comments
  68.     if (m/^([0-9.]+)\s+(\S+)/) {
  69.         $ip{$1}=$2;        # keep track of IP address
  70.     }
  71. }
  72.  
  73. close(HOSTS) || warn "$0: error in $hostfile;$!\n";
  74.  
  75.  
  76. ### Read data from stdin or via snmpwalk
  77.  
  78. # if no arguments were given, read from standard input
  79. # else do an snmpwalk on the named router
  80. #
  81. # Note, it would be faster to walk on just actByts and actPkts
  82. # instead of the whole lipAccountEntry, but it makes the code more
  83. # complex - to be corrected in a future release
  84.  
  85. if ($#ARGV == 1 ) {
  86.     close(STDIN) || die "$0: unable to redirect standard input;$!\n";
  87.     $snmpwalk = "snmpwalk $ARGV[0] $ARGV[1] $mibvar";
  88.     open(STDIN,"$snmpwalk |") || 
  89.         die "$0: unable to spawn snmpwalk; $!\n";
  90.     shift(@ARGV);
  91.     shift(@ARGV);
  92. }
  93. elsif ($#ARGV >= 0 ) {
  94.     warn "@ARGV\n";
  95.     die "usage: $0 [router community]\n";
  96. }
  97.  
  98.  
  99. if ($log) {
  100.     open(TMP,">$logfile") || die "$0: unable to open logfile; $!\n";
  101. }
  102.  
  103. ### now read through data & print table, marking unknown addresses
  104.  
  105. while (<>) {
  106.     print TMP $_ if $log;
  107.     next if (! s/^$prefix//o);
  108.     chop;
  109.     $name = $_;
  110.     
  111.     $_ = <>;    # fetch value
  112.     print TMP $_ if $log;
  113.     chop;
  114.     if (m/(\S+):\s+/) {
  115.         $type = $1;
  116.         $value = $';
  117.     }
  118.  
  119.     # we already know source & destination, so skip those
  120.     # if we were paranoid we could make sure they matched
  121.     next if $name =~ /^act(Src|Dst)\./;
  122.  
  123.     if ($name =~ m/(\D+)\.(\d+\.\d+\.\d+.\d+)\.(\d+\.\d+\.\d+.\d+)/) {
  124.         $marker= ($ip{$2}?" ":$unknown);
  125.         $src=$marker.(($name&&$ip{$2})?$ip{$2}:$2);
  126.         
  127.         $marker= ($ip{$3}?" ":$unknown);
  128.         $dst=$marker.(($name&&$ip{$3})?$ip{$3}:$3);
  129.         $packets{"$src\t$dst"} = $value if $1 eq "actPkts";
  130.         $bytes{"$src\t$dst"} = $value if $1 eq "actByts";
  131.         next;
  132.     }
  133.     # ignore anything else
  134. }
  135.  
  136.  
  137. ### print results - currently sorts alphabetically
  138.  
  139. for $pair (sort(keys(%packets))) {
  140.     ($src,$dst) = split(/\t/,$pair);
  141.     printf "%-15s\t%-15s\t%10u\t%10u\n", $src,$dst,$packets{$pair},$bytes{
  142. $pair};
  143.     $tpackets += $packets{$pair};
  144.     $tbytes += $bytes{$pair};
  145.     $tpair ++;
  146. }
  147.     
  148. printf "%24s\t%10u\t%10u\n", "TOTAL for $tpair host-pairs",$tpackets,$tbytes;
  149.  
  150. if ($log) {
  151.     close(TMP) || die "$0: unable to close log file $logfile ;$!\n";
  152.     warn "$0: snmp log written to $logfile\n";
  153. }
  154.